package com.example.stock.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Objects;

public class DoubleSerialize extends JsonSerializer<Double> {

    private DecimalFormat df = new DecimalFormat("0.##");

    /**
     * @param aDouble 要序列化的 Double 类型数据
     * @param jsonGenerator 用于写入序列化结果的 JsonGenerator 对象
     * @param serializerProvider SerializerProvider 对象，用于提供序列化相关的服务
     * @throws IOException 如果在序列化过程中发生 I/O 错误
     */
    @Override
    public void serialize(Double aDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (!Objects.isNull(aDouble)) {
            jsonGenerator.writeString(df.format(aDouble));
        }
    }

}
